import {
  createRouter,
  createWebHistory,
  createWebHashHistory
} from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { ssoLogin, localLogin } from '@/services/Application'
import { useUserStore } from '@/stores/modules/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { ElMessage } from 'element-plus'

NProgress.configure({ showSpinner: false }) // NProgress Configuration取消右上角旋转图标
const router = createRouter({
  //createWebHistory(import.meta.env.BASE_URL)
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/CourseInquiry',
          component: () => import('@/views/CourseInquiry/index.vue'),
          meta: { title: '课表查询' }
        },
        {
          path: '/RecordingQuery',
          component: () => import('@/views/RecordingQuery/index.vue'),
          meta: { title: '录播查询' }
        }
      ]
    },
    {
      path: '/H5Wapp',
      component: () => import('@/H5Wapp/index.vue'),
      meta: {
        title: 'H5应用'
      }
    }
  ]
})
// 访问权限控制
router.beforeEach(async (to) => {
  document.title = `智慧教学-${to.meta.title || ''}`
  //开启进度条
  NProgress.start()
  // 用户仓库
  const store = useUserStore()
  const url = window.location.href
  const trimmedPath = url.split('/').pop()
  console.log(trimmedPath, 111)

  try {
    if (!store.user?.token) {
      if (import.meta.env.VITE_FLAG === 'por') {
        if (
          trimmedPath === '' ||
          trimmedPath === null ||
          trimmedPath !== 'Travel'
        ) {
          window.location.href =
            'https://jxjyxy.zafu.edu.cn' +
            '/redirectLogin?targetUrl=' +
            '/portal'
        }
      }
    }
  } catch (e) {
    //提示联系管理人员
    ElMessage.error('登录失败，请联系管理员')
  }

  // 不需要登录的页面，白名单
  // const wihteList = ['/login']
  // 如果没有登录且不在白名单内，去登录
  // if (!store.user?.token && !whiteList.includes(to.path)) {
  //   return '/login'
  // }
  // 否则不做任何处理
})
//后置路由守卫
router.afterEach((to) => {
  //关闭进度条
  NProgress.done()
  //修改页面标题
  document.title = `智慧教学-${to.meta.title || ''}`
})
export default router
